package com.cloud.controller;


import com.cloud.Service.MessageService;
import com.cloud.Service.UserService;
import com.cloud.dto.MessageDto;
import com.cloud.dto.UserDto;
import com.cloud.entity.Message;
import com.cloud.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ResponseBody
@Controller
@RequestMapping("/message")
public class MessageController {
    @Autowired
    MessageService messageService;
    @Autowired
    UserService userService;

    /**
     * 发送请求
     * @param
     * @return
     */
    @GetMapping("/send")
    public Map<?, ?> sendMessage( Integer r_id,Integer u_id) {
        UserDto dto = userService.getUserById(u_id);
        System.out.println(dto);
        //需要获取 当前用户id! 此处2为假定

        HashMap<Object, Object> map = new HashMap<>();
        MessageDto message = messageService.sendMessage(r_id, dto);
        map.put("message", message);
        return map;
    }

    /**
     * 接受请求
     * @param m_id
     * @return String
     */
    @PostMapping("/accept")
    public Map<?, ?> accept(Integer m_id,Integer u_id) {
        MessageDto dto = messageService.getMessageById(m_id);
        Message message = new Message();
        BeanUtils.copyProperties(dto, message);

        UserDto userdto = userService.getUserById(u_id);
        //当前用户id 此处50为假定

        User user = new User();
        BeanUtils.copyProperties(userdto, user);

        userService.accept(user.getUserId(), message.getTeamId());
        messageService.update(message);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("info", "接受邀请");

        return map;
    }


    /**
     * 根据id获取message
     */
    @GetMapping("/get/{id}")
    public Map<?,?> getMessageById(@PathVariable("id") Integer Id) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("message",messageService.getMessageById(Id));
        return map;
    }


    /**
     * 拒绝
     *
     * @param m_id
     * @return
     */
    @GetMapping("/refuse/{id}")
    public Map<?,?> refuse(@PathVariable("id") int m_id) {
        MessageDto dto = messageService.getMessageById(m_id);
        Message message = new Message();
        BeanUtils.copyProperties(dto, message);
        messageService.update(message);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("info","拒绝");
        return map;
    }


//    /**
//     * 所有消息 -- 管理员
//     *
//     * @return
//     */
//    @GetMapping("/allmessage")
//    public Map<?,?> getall() {
//        List<MessageDto> messageDtos = messageService.getall();
//
//        return ;
//    }


    @GetMapping("/unread/{userid}")
    public Map<?,?> unread(@PathVariable("userid") int userid) {
        List<MessageDto> messageDtos = messageService.unread(userid);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("messages",messageDtos);
        return map;
    }


    @GetMapping("/histroy/{userid}")
    public Map<?,?> histroy(@PathVariable("userid") int userid) {
        List<MessageDto> messageDtos = messageService.histroy(userid);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("messages",messageDtos);
        return map;
    }


}
